package product

import (
	"context"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"gorm.io/gen"
	"gorm.io/gen/field"
	"mall_admin_api/biz/dal"
	"mall_admin_api/biz/handler/common"
	"mall_admin_api/biz/model"
	"mall_admin_api/biz/model/admin_product_category"
	"mall_admin_api/biz/pack"
)

type Category struct {
	commonInfo *common.Response
	dao        *dal.InitDAO
}

func NewCategory(response *common.Response) *Category {
	return &Category{
		commonInfo: response,
		dao:        dal.NewInitDAO(response.GetBizInfo().ID),
	}
}

func (c *Category) GetList(ctx context.Context, req *admin_product_category.GetCategoryListRequest) ([]*admin_product_category.Category, error) {
	var conditions []gen.Condition
	conditions = append(conditions, dal.Category.BizID.Eq(c.commonInfo.GetBizInfo().ID))
	if req.GetName() != "" {
		conditions = append(conditions, dal.Category.Name.Like("%"+req.GetName()+"%"))
	}
	list, err := c.dao.CategoryDAO.GetCategoryList(ctx, conditions, 0, 10000)
	if err != nil {
		hlog.CtxErrorf(ctx, "GetCategoryList err: %v", err)
		return nil, err
	}
	return pack.GetCategoryList(list), nil
}

func (c *Category) GetSelectCategoryList(ctx context.Context, req *admin_product_category.GetSelectCategoryListRequest) ([]*admin_product_category.SelectCategory, error) {
	var conditions []gen.Condition
	conditions = append(conditions, dal.Category.BizID.Eq(c.commonInfo.GetBizInfo().ID))
	list, err := c.dao.CategoryDAO.GetCategoryList(ctx, conditions, 0, 10000)
	if err != nil {
		hlog.CtxErrorf(ctx, "GetCategoryList err: %v", err)
		return nil, err
	}
	return pack.GetSelectCategoryList(list), nil
}

func (c *Category) Update(ctx context.Context, req *admin_product_category.UpdateCategoryRequest) error {
	var fields []field.AssignExpr
	fields = append(fields, dal.Category.UpdatedBy.Value(c.commonInfo.GetAdministratorInfo().Nickname))
	if req.Name != nil {
		fields = append(fields, dal.Category.Name.Value(req.GetName()))
	}
	if req.Sort != nil {
		fields = append(fields, dal.Category.Sort.Value(req.GetSort()))
	}
	if req.PID != nil {
		fields = append(fields, dal.Category.Pid.Value(req.GetPID()))
	}
	if err := c.dao.CategoryDAO.UpdateCategory(ctx, []gen.Condition{dal.Category.ID.Eq(req.ID)}, fields); err != nil {
		hlog.CtxErrorf(ctx, "Update err: %v", err)
		return err
	}
	return nil
}

func (c *Category) Delete(ctx context.Context, req *admin_product_category.DeleteCategoryRequest) error {
	if err := c.dao.CategoryDAO.DeleteCategory(ctx, []gen.Condition{dal.Category.ID.In(req.GetIDs()...)}); err != nil {
		hlog.CtxErrorf(ctx, "Delete err: %v", err)
		return err
	}
	return nil
}

func (c *Category) Create(ctx context.Context, req *admin_product_category.CreateCategoryRequest) error {
	var data = &model.Category{
		Name:      req.Name,
		BizID:     c.commonInfo.GetBizInfo().ID,
		Pid:       req.PID,
		Sort:      req.Sort,
		CreatedBy: c.commonInfo.GetAdministratorInfo().Nickname,
	}
	if _, err := c.dao.CategoryDAO.CreateCategory(ctx, data); err != nil {
		hlog.CtxErrorf(ctx, "Create err: %v", err)
		return err
	}
	return nil
}
